package com.aaa.sts.controller;

import com.aaa.common.entity.Store;
import com.aaa.common.util.BaseUtil;
import com.aaa.common.util.OssUtil;
import com.aaa.common.util.Result;
import com.aaa.common.vo.Page;
import com.aaa.common.vo.StoreRegisterStatisticsVO;
import com.aaa.sts.service.StoreService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Store)表控制层
 *
 * @author makejava
 * @since 2025-09-16 09:55:22
 */
@RestController
@RequestMapping("store")
@Log4j2
public class StoreController {
    /**
     * 服务对象
     */
    @Resource
    private StoreService storeService;

    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile file){
        //调用工具类，进行上传
        return BaseUtil.success(OssUtil.uploadFile("store/images",file));
    }

    @GetMapping("selectStoreName")
    public Result selectStoreName(@RequestParam Integer storeId) {
        return BaseUtil.success(storeService.selectStoreName(storeId));
    }

    /**
     * 查询店铺绑定人数
     *
     * @param storeId
     * @return
     */
    @GetMapping("/queryByUserId")
    public Result queryByUserId(Integer storeId) {
        return BaseUtil.success(storeService.queryByUserId(storeId));
    }

    @GetMapping("selectAllStore")
    public Result selectAllStore(@RequestParam(required = false) String storeName) {
        List<Store> stores = storeService.selectAllStore(storeName);
        return BaseUtil.success(stores);
    }

    //    @GetMapping("/selectStoreByStoreIdAndUserId")
//    public Store selectStoreByStoreIdAndUserId(@RequestParam Integer storeId,
//                                               @RequestParam Integer userId) {
//        return storeService.selectStoreByStoreIdAndUserId(storeId, userId);
//    }
    @GetMapping("/selectStoreByStoreIdAndUserId")
    public List<Store> selectStoreByStoreIdAndUserId(@RequestParam(required = false) Integer storeId,
                                                     @RequestParam(required = false) Integer userId) {
        return storeService.selectStoreByStoreIdAndUserId(storeId, userId);
    }

    @GetMapping("/selectStoreById")
    public List<Store> selectStoreById() {
        return storeService.selectStoreById();
    }

    /**
     * 按月份统计商家入驻数量
     */
    @GetMapping("/selectStoreNumberByMonth")
    public Result selectStoreNumberByMonth() {
        List<StoreRegisterStatisticsVO> statistics = storeService.selectStoreNumberByMonth();
        return BaseUtil.success(statistics);
    }

    /**
     * 查询商家入驻总数
     */
    @GetMapping("/selectAllStoreNumber")
    public Result selectAllStoreNumber() {
        Integer total = storeService.selectAllStoreNumber();
        return BaseUtil.success(total);
    }

    /**
     * 查询矩形区域内的店铺列表
     *
     * @param rectangle 矩形区域参数，格式：minLng,minLat;maxLng,maxLat
     * @param address   地址参数（预留，当前未使用）
     * @return 店铺列表
     */
    @GetMapping("/selectStoresList")
    public Result<List<Store>> selectStoresList(
            @RequestParam String rectangle,
            @RequestParam(required = false) String address
    ) {
        try {
            // 解析rectangle参数
            String[] parts = rectangle.split(";");
            if (parts.length != 2) {
                return BaseUtil.fail("参数格式错误，正确格式应为: minLng,minLat;maxLng,maxLat");
            }

            // 解析最小经纬度
            String[] minCoords = parts[0].split(",");
            if (minCoords.length != 2) {
                return BaseUtil.fail("参数格式错误，正确格式应为: minLng,minLat;maxLng,maxLat");
            }

            // 解析最大经纬度
            String[] maxCoords = parts[1].split(",");
            if (maxCoords.length != 2) {
                return BaseUtil.fail("参数格式错误，正确格式应为: minLng,minLat;maxLng,maxLat");
            }

            // 转换为数值类型
            Float minLng = Float.parseFloat(minCoords[0]);
            Float minLat = Float.parseFloat(minCoords[1]);
            Float maxLng = Float.parseFloat(maxCoords[0]);
            Float maxLat = Float.parseFloat(maxCoords[1]);

            // 调用服务层查询店铺，传入address参数进行筛选
            List<Store> stores;
            if (address != null && !address.trim().isEmpty()) {
                // 如果提供了地址参数，则同时按地理范围和地址筛选
                stores = storeService.getStoresInRectangleAndAddress(
                        minLng, maxLng, minLat, maxLat, address.trim()
                );
            } else {
                // 如果没有地址参数，只按地理范围筛选
                stores = storeService.getStoresInRectangle(minLng, maxLng, minLat, maxLat);
            }

            return BaseUtil.success(stores);
        } catch (NumberFormatException e) {
            return BaseUtil.fail("参数格式错误，经纬度必须为数字");
        } catch (Exception e) {
            return BaseUtil.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("selectMySelfStore")
    public Result queryByPage(@RequestBody Page<Store> page) {
        return BaseUtil.success(this.storeService.queryByPage(page));
    }

    @PostMapping("/selectMyStore")
    public List<Store> selectMyStore(@RequestBody Store store) {
        return this.storeService.selectMyStore(store);
    }

    /**
     * 分页查询,查询店铺名称
     *
     * @param page 分页对象
     * @return 店铺名称
     */
    @PostMapping("queryName")
    public Result queryName(@RequestBody Page<Store> page) {
        return BaseUtil.success(this.storeService.queryByPage(page));
    }
    /**
     * 分页查询
     *
     * @param
     * @return 店铺名称
     */
    /*@PostMapping("selectMySelfStore")
    public Result selectMySelfStore(@RequestBody StoreQueryDTO queryDTO) {
        return BaseUtil.success(storeService.selectMySelfStore(queryDTO));
    }*/

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(@RequestParam("id") Integer id) {
        return BaseUtil.success(this.storeService.queryById(id));
    }

    @GetMapping("queryById1")
    public Store queryById1(@RequestParam("id") Integer id) {
        return this.storeService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param store 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public Result add(@RequestBody Store store) {
        return BaseUtil.success(this.storeService.insert(store));
    }

    /**
     * 编辑数据
     *
     * @param store 实体
     * @return 编辑结果
     */
    @PostMapping("update")
    public Result edit(@RequestBody Store store) {
        return BaseUtil.success(this.storeService.update(store));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.storeService.deleteById(id));
    }

}

